﻿/**
 ** @作者:	   王桂龙 QQ731513122
 ** @日期:	   2019.12.6
 ** @简介:     ListBox待我想想怎么搞这个      
 */
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;

namespace BHXJDOTNET.OLD
{
    public class BHXJListBox:ListBox
    {

        ColorBlend colorblendNormal = new ColorBlend(4);
        ColorBlend colorBlendSelected = new ColorBlend(4);
        public BHXJListBox()
        {
            this.SetStyle(ControlStyles.UserPaint, true);
            this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
            this.SetStyle(ControlStyles.DoubleBuffer, true);
            this.SetStyle(ControlStyles.ResizeRedraw, true);
            this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
            
            this.SelectedIndexChanged+=new EventHandler(BHXJListBox_SelectedIndexChanged);
            

            //防止字体在设计器中添加该控件时候自动追随父控件
            this.Font = this.Font;

            this.DrawMode = DrawMode.OwnerDrawFixed;
            this.BorderStyle = BorderStyle.None;
            this.BackColor = Color.FromArgb(64,64,64);
            getBlend();
        }
        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            Rectangle disArea = this.DisplayRectangle;
            Graphics gs = e.Graphics;

            int itemwidth = 0;
            if (this.Items.Count > 0)
            {
                Rectangle recBounds = this.GetItemRectangle(0);
                itemwidth = recBounds.Width;
            }
            else
            {
                itemwidth = this.Width;
            }

            LinearGradientBrush lgb = new LinearGradientBrush(new Point(0, 0), new Point(itemwidth,0), Color.White, Color.Black);

            lgb.InterpolationColors = this.colorblendNormal;

            Brush textBrush = new SolidBrush(this.ForeColor);
            //背景
            gs.FillRectangle(lgb, 0, 0, this.Width, this.Height);

            for (int i = 0; i < this.Items.Count; ++i)
            {
                if (this.SelectedIndex == i)
                    lgb.InterpolationColors = this.colorBlendSelected;
                else
                    lgb.InterpolationColors = this.colorblendNormal;
                

                this.PaintRectangleItem(gs, i, lgb, textBrush);
            }
        }

        private void PaintRectangleItem(Graphics gs, int index, LinearGradientBrush brush, Brush FontBrush)
        {
            Rectangle recBounds = this.GetItemRectangle(index);
            RectangleF tabTextArea = (RectangleF)recBounds;
            StringFormat stringFormat = new StringFormat();
            stringFormat.Alignment = StringAlignment.Center;
            stringFormat.LineAlignment = StringAlignment.Center;

            gs.FillRectangle(brush, recBounds.Location.X, recBounds.Location.Y, recBounds.Width, recBounds.Height);

            gs.DrawString(this.Items[index].ToString(), this.Font, FontBrush, tabTextArea, stringFormat);
            
        }
        private void BHXJListBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.Invalidate();
        }
        private void getBlend()
        {
            Color ncolor0 = Color.FromArgb(73, 165, 236);
            Color ncolor1 = Color.FromArgb(80, 181, 255);

            Color scolor0 = Color.FromArgb(65, 148, 212);
            Color scolor1 = Color.FromArgb(71, 162, 233);

            //Color ncolor0 = Color.FromKnownColor(KnownColor.Control);
            //Color ncolor1 = ncolor0;

            //Color scolor0 = Color.FromArgb(200, 200, 200);
            //Color scolor1 = Color.FromArgb(200, 200, 200);

            Color[] ncolors = 
            {  ncolor0,
                ncolor1,
                ncolor1,
                ncolor0
            };
            Color[] scolors = 
            {  
                scolor0,
                scolor1,
                scolor1,
                scolor0
            };
            float[] positions = { 0.0f, 0.2f,0.8f, 1.0f };
            colorblendNormal = new ColorBlend(4);
            colorblendNormal.Colors = ncolors;
            colorblendNormal.Positions = positions;


            colorBlendSelected = new ColorBlend(4);
            colorBlendSelected.Colors = scolors;
            colorBlendSelected.Positions = positions;
        }
    }
}
